<?php
namespace app\serverarticle\logic;

use think\Db;
use \app\manage\logic\BaseLogic;

/**
 * 文章微应用下的逻辑层
 * @author  蒋哲 2017.5.23
 */
class ArticleClassify extends BaseLogic
{
    protected $name = 'article_classify';
    protected $config = 'db_config_article';
    protected $classifyModel = 'app\serverarticle\model\ArticleClassify';

    /*根据id获取其父类的ids数组*/
    public function getParentIds($id): array 
    {
        $list = $this->db()->where(['status'=>['neq',-1]])->select();
        $ids     = $id;
        $params  = 'id';
        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
        $ids = explode(",", $ids);
        foreach ($ids as $k => &$v) {
            $v = (int)$v;
        }
        array_splice($ids, array_search(0, $ids), 1);
        return array_reverse($ids);
    }

    // 文章分类添加
    public function classifyAdd($data)
    {
        $rst = $this->model($this->classifyModel)->save($data);
        $rstId = $this->model($this->classifyModel)->id;

        if ($rst === false) {
            return auto_failure ($data,'文章分类添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章分类未作改动！');
        }
        return auto_return(['id' => $rstId],'分类添加成功！');
    }

    // 某条文章分类信息编辑修改
    public function classifyEdit($data)
    {
        $data['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($data);
        if ($rst === false) {
            return auto_failure ($data,'分类信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类信息未作改动！');
        }

        return auto_return(['number' => $rst],'分类信息更改成功！');
    }

    // 启用某个分类文章分类
    public function classifyEnable($data): array
    {
        $res = $this->db()->where($data)->find();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 禁用某个分类文章分类
    public function classifyDisable($data): array
    {
        $res = $this->db()->where($data)->find();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = 0;
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 删除某个分类文章分类
    public function classifyDelete($data): array
    {
        $res = $this->db()->where($data)->find();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = -1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }
        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    // 全部文章分类列表获取
    public function classifyList()
    {
        $_where["status"] = array("neq", -1);
        $manageAppKey = \app\manage\service\ApiBase::getAppKey();
        $_whereOr = $manageAppKey == '4942b00d6a75eebcce692224b0cb3110' ? true : '`key` = "' . $manageAppKey . '"';
        $arr = $this->db()->where($_where)->where($_whereOr)->order('id', 'desc')->select();
        foreach ($arr as $k => &$v) {
            $v['status_text'] = $v['status'] == 1 ? '正常' : '禁用';
        }
        //$list = _make_tree(mosaic_child($arr));
        $list = list_to_tree($arr,'id','pid','_child', 0);
        return auto_return($list);
    }

    // 获取某个分类文章分类 非删除
    public function classifySomeList($data): array
    {
        $condition['id'] = $data['pid'];
        $condition['key'] = $data['key'];
        $res = $this->db()
                    ->where($condition)
                    ->find();
        if (empty($res)) {
            return null_data($res,"分类的id和标识不匹配，请重试！");
        }
        $_where['status'] = ['eq',1];
        $_where['pid'] = $data['pid'];
        $arr = $this->db()
                    ->where($_where)
                    ->order('id', 'desc')
                    ->select();

        $list = mosaic_child($arr);
        return auto_return($list);
    }

    // 不同条件文章分类列表获取 非删除 分类名称 描述
    public function classifyCondition($data): array
    {
        $_where['status'] = array('eq','1');
        $_where['name|description']=array('like',"%".$data['keywords']."%");
        $arr = $this->db()
                    ->where($_where)
                    ->order('id', 'desc')
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($arr);
    }
}
